# coding: utf-8
from typing import List

from PySide6.QtCore import Signal, Qt
from PySide6.QtGui import QResizeEvent
from PySide6.QtWidgets import QWidget, QVBoxLayout
from qfluentwidgets import FluentIcon, TransparentToolButton, TransparentPushButton, TitleLabel, \
    ToolTipFilter, setFont, Pivot

from ...common import signalBus, BookItemModel, cfg, getThemeColor
from ...components import VerticalScrollArea, GridLayout, HorizontalScrollArea, ImageLabel


class TopBaseInterface(VerticalScrollArea):
    closed = Signal()

    def __init__(self, parent=None):
        super().__init__(parent)
        self.closeBtn = TransparentToolButton(FluentIcon.RETURN, self)

        self.setObjectName('TopBaseInterface')
        self.closeBtn.setGeometry(5, 5, 40, 36)
        self.closeBtn.clicked.connect(self.closed)


class ChapterWidget(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.verticalLayout = QVBoxLayout(self)
        self.horizontalScrollArea = HorizontalScrollArea(self)
        self.pivot = Pivot(self)
        self.gridLayout = GridLayout()
        self.bodyLabel = TitleLabel('章节目录', self)

        self.horizontalScrollArea.setFixedHeight(40)
        self.horizontalScrollArea.setWidget(self.pivot)
        self.horizontalScrollArea.setAlignment(Qt.AlignmentFlag.AlignLeft)
        self.horizontalScrollArea.setHorizontalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAlwaysOff)

        self.gridLayout.setItemMinimumWidth(200)
        self.verticalLayout.setAlignment(Qt.AlignmentFlag.AlignTop)
        self.verticalLayout.setContentsMargins(32, 16, 32, 16)
        self.verticalLayout.addWidget(self.bodyLabel)
        self.verticalLayout.addWidget(self.horizontalScrollArea)
        self.verticalLayout.addLayout(self.gridLayout, 1)

        self.pivot.currentItemChanged.connect(self.onItemClicked)

    def onItemClicked(self, routeKey: str):
        self.gridLayout.takeAllWidgets()
        models = self.pivot.items[routeKey].property('models')
        md_list = self.property('models')
        useBrowser = self.property('useBrowser')
        for model in models:
            btn = TransparentPushButton(model.title, self)
            btn.setCursor(Qt.CursorShape.PointingHandCursor)
            setFont(btn, 12)
            btn.setMinimumSize(200, 33)
            btn.setToolTip(model.title)
            btn.installEventFilter(ToolTipFilter(btn))
            if useBrowser:
                btn.clicked.connect(lambda x, m=model: signalBus.webUrlSignal.emit(m))
            else:
                btn.clicked.connect(lambda x, m=model: signalBus.photoSignal.emit(m, md_list))
            self.gridLayout.addWidget(btn)

    def setModel(self, models: List[BookItemModel]):
        self.setProperty('models', models)
        n = 50  # 表示每页显示的数量
        for i in range(0, len(models), n):
            models_ = models[i:i + n]
            item = self.pivot.addItem(str(i), '%d - %d' % (i + 1, i + n))
            item.setProperty('models', models_)
        self.pivot.setItemFontSize(14)
        self.pivot.setCurrentItem('0')

    def clear(self):
        self.gridLayout.takeAllWidgets()
        self.pivot.clear()
